package com.secondhand.controller;

import com.secondhand.domain.order;
import com.secondhand.service.Impl.orderServiceImpl;
import com.secondhand.utils.AjaxResult;
import com.secondhand.vo.orderItem;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api("二手交易相关接口")
@Log4j2
@RestController
@RequestMapping("/order")
public class orderController {
    @Autowired
    orderServiceImpl orderService;

    @ApiOperation("生成订单")
    @PostMapping("/generate")
    public AjaxResult orderGenerate(@RequestBody order orders) {
        boolean result = orderService.generateOrder(orders);
        return AjaxResult.success("生成订单",result);
    }

    @ApiOperation("已卖出")
    @ApiImplicitParam(name = "userId", value = "用户Id", required = true, dataType = "int")
    @GetMapping("/sold")
    public AjaxResult getSold(@RequestParam("userId") int userId) {
        List<orderItem> result = orderService.getSold(userId);
        return AjaxResult.success("已卖出",result);
    }

    @ApiOperation("已购买")
    @ApiImplicitParam(name = "userId", value = "用户Id", required = true, dataType = "int")
    @GetMapping("/bought")
    public AjaxResult getBought(@RequestParam("userId") int userId) {
        List<orderItem> result = orderService.getBought(userId);
        return AjaxResult.success("已购买",result);
    }

    @ApiOperation("卖家未完成订单")
    @ApiImplicitParam(name = "userId", value = "用户Id", required = true, dataType = "int")
    @GetMapping("/halfSoldOrder")
    public AjaxResult getHalfSoldOrder(@RequestParam("userId") int userId) {
        List<orderItem> result = orderService.getHalfSoldOrder(userId);
        return AjaxResult.success("卖家未完成订单",result);
    }

    @ApiOperation("买家未完成订单")
    @ApiImplicitParam(name = "userId", value = "用户Id", required = true, dataType = "int")
    @GetMapping("/halfBoughtOrder")
    public AjaxResult getHalfBoughtOrder(@RequestParam("userId") int userId) {
        List<orderItem> result = orderService.getHalfBoughtOrder(userId);
        return AjaxResult.success("买家未完成订单",result);
    }

    @ApiOperation("发货")
    @ApiImplicitParam(name = "orderId", value = "订单Id", required = true, dataType = "int")
    @GetMapping("/send")
    public AjaxResult send(@RequestParam("orderId") int orderId) {
        boolean result = orderService.send(orderId);
        return AjaxResult.success("发货完成",result);
    }

    @ApiOperation("签收")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单Id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "goodId", value = "商品Id", required = true, dataType = "int")
    })
    @GetMapping("/receive")
    public AjaxResult receive(@RequestParam("orderId") int orderId,@RequestParam("goodId") int goodId) {
        boolean result = orderService.receive(orderId,goodId);
        return AjaxResult.success("签收完成",result);
    }
}
